package com.hdjk.view.receiver;

import com.hdjk.view.config.Constant;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class MyNetworkChangeReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		ConnectivityManager manager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mobileInfo = manager
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		NetworkInfo wifiInfo = manager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		NetworkInfo activeInfo = manager.getActiveNetworkInfo();
		
		boolean isConnected = false;
		// 网络状态在移动网络和无线网络之间切换时，发送广播
		if ((mobileInfo != null && mobileInfo.isConnected())
				|| (wifiInfo != null && wifiInfo.isConnected())
				|| (activeInfo != null && activeInfo.isConnected())) {
			isConnected = true;
		} else {
			isConnected = false;
		}
		
		Intent intentChange = new Intent(
				Constant.INTENT_FILTER_STRING_AFTER_NETWORK_CHANGE);
		intentChange.putExtra("is_net_connected", isConnected);
		context.sendBroadcast(intentChange);
	}

}